#Include ..\Extend_Merged.ahk

class Button {

  events := Map()
  static ON_PRESSED := 0x1, ON_RELEASE := 0x2

  __New(_rect, bg, mg) {
    this._rect := _rect, this.bg := bg, this.mg := mg
  }

  __Get(key, Params) => this[key]

  SetBG(color) => this.bg := color
  SetFG(color) => this.fg := color

  OnEvent(event, cb) => this.events.Set(event, cb)

  OnPressed() {
    this.pressed := 1
    this.events.Get(Button.ON_PRESSED, Noop)(this)
  }

  OnRelease(trigger := 0) {
    if !this.pressed
      return
    this.pressed := 0
    if trigger
      this.events.Get(Button.ON_RELEASE, Noop)(this)
  }

  Draw(canvas, dx := 0, dy := 0) {
    c := this.pressed ? this.mg : this.bg
    canvas.FillRectangle(this._rect.x + dx, this._rect.y + dy, this._rect.w, this._rect.h, c)
  }
}